package com.atguiug.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.atguigu.servicebase.exceptionhandler.GuiliException;
import com.atguiug.vod.Utils.ConstantVodUtils;
import com.atguiug.vod.Utils.InitVodCilent;
import com.atguiug.vod.service.VodService;
import common.atguigu.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/eduvod/video")
@CrossOrigin
@Api(description = "视频管理")
public class VodController {

    @Autowired
    private VodService vodService;

    //上传视频到阿里云

    @PostMapping("/uploadAlyiVideo")
    @ApiOperation(value = "上传视频到阿里云")
    public R uploadAlyiVideo(MultipartFile file) {
        //返回上传视频id
        String videoId = vodService.uploadVideoAly(file);
        return R.ok().data("videoId", videoId);
    }

    //根据视频id删除阿里云视频
    @DeleteMapping("/removeAlyVideo/{id}")
    public R removeAlyVideo(@PathVariable String id) {
        try {
            //初始化对象
            DefaultAcsClient client = InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建删除视频的对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向requset中设置视频Id
            request.setVideoIds(id);
            //调用初始化对象的方法进行删除
            client.getAcsResponse(request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuiliException(20001, "删除课程失败");
        }
    }

    //删除多个阿里云视频
    //删除多个视频id List<String> videoIdList
    @RequestMapping("/delete-batch")
    public R deleteBatch(@RequestParam("videoIdList") List<String> videoIdList){
        vodService.removeMoreAlyVideo(videoIdList);
        return R.ok();
    }
}
